﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using DigitalSolutionLibrary.SqlDatabase;
using DigitalSolutionLibrary.Utilities;
using System.IO;

namespace DigitalSolutionManagement
{
    public partial class frmComInfo : BaseFormEdit
    {
        private Thread th;

        public override BaseFormEdit GetNewInstance(BaseFormEdit.OpenMode mode)
        {
            return new frmComInfo();
        }

        public frmComInfo()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
        }

        private void LoadComInfo()
        {
            object obj = Program.dbWrapper.ExecuteStore("LoadComInfo", null, DatabaseWrapper.ReturnType.DATASET);
            if (obj != null)
            {
                DataSet ds = (DataSet)obj;
                txtAddress.Text = ds.Tables[0].Rows[0]["ComAddress"].ToString();
                txtEmail.Text = ds.Tables[0].Rows[0]["ComEmail"].ToString();
                txtFax.Text = ds.Tables[0].Rows[0]["ComFax"].ToString();
                txtName.Text = ds.Tables[0].Rows[0]["ComName"].ToString();
                txtPhone.Text = ds.Tables[0].Rows[0]["ComPhone"].ToString();
                txtSite.Text = ds.Tables[0].Rows[0]["ComSite"].ToString();
                if (ds.Tables[0].Rows[0]["ComLogo"] != DBNull.Value)
                {
                    picLogo.BackgroundImage = null;
                    picLogo.Image = Utility.ByteToImage((byte[])ds.Tables[0].Rows[0]["ComLogo"]);
                }
                processing1.HideProcessing();
            }
            else
            {
                processing1.HideProcessing();
                MessageBox.Show("Không thể load thông tin, vui lòng thử lại sau", "DSCo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                btnOk.Enabled = false;
            }
        }

        private void UpdateComInfo()
        {
            if (txtName.Text == String.Empty)
            {
                processing1.HideProcessing();
                MessageBox.Show("Vui lòng nhập tên công ty", "DSCo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtName.Focus();
                return;
            }

            if (txtAddress.Text == String.Empty)
            {
                processing1.HideProcessing();
                MessageBox.Show("Vui lòng nhập địa chỉ công ty", "DSCo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtAddress.Focus();
                return;
            }

            ParameterStock stock = new ParameterStock();
            Parameter par = new Parameter("name", txtName.Text, SqlDbType.NVarChar, 100);
            stock.AddParamter(par);

            par = new Parameter("address", txtAddress.Text, SqlDbType.NVarChar, 100);
            stock.AddParamter(par);

            par = new Parameter("phone", txtPhone.Text, SqlDbType.NVarChar, 20);
            stock.AddParamter(par);

            par = new Parameter("fax", txtFax.Text, SqlDbType.NVarChar, 20);
            stock.AddParamter(par);

            par = new Parameter("email", txtEmail.Text, SqlDbType.NVarChar, 100);
            stock.AddParamter(par);

            par = new Parameter("site", txtSite.Text, SqlDbType.NVarChar, 100);
            stock.AddParamter(par);

            if (picLogo.Image != null)
            {
                par = new Parameter("logo", Utility.ImageToByte(picLogo.Image), SqlDbType.Image);
                stock.AddParamter(par);
            }

            object obj = Program.dbWrapper.ExecuteStore("EditComInfo", stock, DatabaseWrapper.ReturnType.FIRST_CELL);
            if (obj != null)
            {
                DialogResult = System.Windows.Forms.DialogResult.OK;
            }
            else
            {
                processing1.HideProcessing();
                MessageBox.Show("Không thể kết nối đến CSDL, vui lòng thử lại sau", "DSCo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void frmComInfo_Load(object sender, EventArgs e)
        {
            processing1.ShowProcessing();
            th = new Thread(() => LoadComInfo());
            th.Start();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            processing1.ShowProcessing();
            th = new Thread(() => UpdateComInfo());
            th.Start();
        }

        private void picLogo_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                FileInfo finfo = new FileInfo(openFileDialog1.FileName);
                if (finfo.Length > 1024 * 100)
                {
                    MessageBox.Show("Dung lượng ảnh quá lớn, vui lòng chọn ảnh nhỏ hơn 100KB", "DSCo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    picLogo.BackgroundImage = null;
                    picLogo.ImageLocation = openFileDialog1.FileName;
                }
            }
        }

        private void lblClear_Click(object sender, EventArgs e)
        {
            if (picLogo.Image!=null)
            {
                picLogo.Image = null;
                picLogo.BackgroundImage = Properties.Resources.no_image;
            }
        }
    }
}
